package com.dunqi.gpm.shunqing.my.util;

import com.dunqi.gpm.shunqing.common.base.Data3;
import com.dunqi.gpm.shunqing.common.util.JsonUtils;
import com.dunqi.gpm.shunqing.common.util.SharedPreferencesUtils;
import com.dunqi.gpm.shunqing.my.bean.User;

import java.util.Map;

/**
 * Created by Administrator on 2017/11/25.
 */

public class UserUtil {


    private static User user;

    private static final String TAG = "UserUtil";

    public static void saveUser(User newUser) {
        if (newUser == null) {
            return;
        }
        user = newUser;

        String userString = JsonUtils.toJson(newUser);

        SharedPreferencesUtils.putShareData(TAG, userString);

    }

    public static String getBaseUrl() {
        User user = getUser();
        if (user == null) {
            return "";
        }
        return getUser().getBaseUrl();
    }

    public static User getUser() {
        if (user == null) {
            String userString = SharedPreferencesUtils.getShareData(TAG);
            User newUser = JsonUtils.fromJson(userString, User.class);
            user = newUser;
        }
        return user;
    }

    public static void clear() {
        user = null;
        SharedPreferencesUtils.putShareData(TAG, "");
    }

    public static int getUserId() {
        return getUser() == null ? -1 : user.getUserId();
    }


    public static String getAppToken() {
        return getUser() == null ? null : getUser().getAppToken();
    }

    public static int getLevelType() {
        Map<String, Data3> userRoles = getUser().getUserRoles();

        if (userRoles.size() == 0) {
            return -1;
        }

//       resp_leader、ndrc_leader、main_leader

        if (userRoles.containsKey("resp_leader") || userRoles.containsKey("ndrc_leader") || userRoles.containsKey("main_leader")) {
            return 1;
        }

        if (userRoles.containsKey("ndrc_admin")) {
            return 2;
        }

        if (userRoles.containsKey("resp_admin")) {
            return 3;
        }

        return -1;
    }

    public static Boolean getUDA() {
        return userFromRespOrg();
    }

    //责任单位 3
    public static Boolean userFromRespOrg() {
        Map<String, Data3> userRoles = getUser().getUserRoles();
        return userRoles.containsKey("resp_admin") || userRoles.containsKey("resp_leader");
    }

    //区发改委  2
    public static Boolean userFromNdrcOrg() {
        Map<String, Data3> userRoles = getUser().getUserRoles();
        return userRoles.containsKey("ndrc_admin") || userRoles.containsKey("ndrc_leader");
    }

    //领导 1
    public static Boolean userIsLeader() {
        Map<String, Data3> userRoles = getUser().getUserRoles();
        return userRoles.containsKey("resp_leader") || userRoles.containsKey("ndrc_leader") || userRoles.containsKey("main_leader");
    }

    public static long getOrgId() {
        User user = getUser();
        return user == null ? -1 : user.getOrgId();
    }
//    roleCode 角色权限
//    sysadmin 系统管理员
//    main_leader 主要领导 县级、区级领导
//    ndrc_admin 发改局管理员
//    resp_admin 责任单位管理员
//    pm_leader 分管领导
}
